//
//  FoodViewController.m
//  WhereToTravel
//
//  Created by dlios on 15/10/19.
//  Copyright © 2015年 zhoushengmin. All rights reserved.
//

#import "FoodViewController.h"
#import "CityModel.h"
#import "DisCountWebView.h"
#import "PlayCell.h"

@interface FoodViewController ()<UITableViewDataSource,UITableViewDelegate,DZNEmptyDataSetSource,DZNEmptyDataSetDelegate>

@property (nonatomic, strong) NSMutableArray *foodArr;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UILabel *titleLabel;

@end

@implementation FoodViewController

// 视图的出现将tabbar隐藏
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
}

- (void)creatData {
    [ZSMAFNetWork geturl:self.fUrl body:nil result:ZSMJSON headerFile:nil success:^(id result) {
        NSArray *arr = result[@"data"][@"entry"];
        self.foodArr = [NSMutableArray array];
        for (NSDictionary *dic in arr) {
            CityModel *city = [[CityModel alloc]initWithDic:dic];
            [self.foodArr addObject:city];
        }
        [self.tableView reloadData];
        self.hud.hidden = YES;
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
        
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(night) name:@"night" object:nil];
    
    [self creatData];
    self.titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100 * WIDTH, 30 * HEIGHT)];
    self.titleLabel.textColor = [UIColor whiteColor];
    self.titleLabel.text = @"美食";
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    self.titleLabel.font = [UIFont boldSystemFontOfSize:20];
    self.navigationItem.titleView = self.titleLabel;

    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH * 375, HEIGHT * 667) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.emptyDataSetSource = self;
    self.tableView.emptyDataSetDelegate = self;
    self.tableView.tableFooterView = [UIView new];
    self.tableView.rowHeight = HEIGHT * 100;
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.equalTo(self.view);        
    }];
    
    self.hud = [MBProgressHUD showHUDAddedTo:self.tableView animated:YES];
    [self.hud hide:YES afterDelay:2];
    // Do any additional setup after loading the view.
}
- (UIView *)customViewForEmptyDataSet:(UIScrollView *)scrollView {
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(20, self.view.bounds.size.height/2, WIDTH*375, 30)];
    label.text = @"! 抱歉,没有内容";
    label.textAlignment = NSTextAlignmentCenter ;
    label.textColor = [UIColor redColor];
    return label;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.foodArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString  *reuse = @"foodCell";
    PlayCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell = [[PlayCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = [UIColor clearColor];
    }
    CityModel *city = self.foodArr[indexPath.row];
    [cell.leftImage sd_setImageWithURL:[NSURL URLWithString:city.photo] placeholderImage:[UIImage imageNamed:@"F"]];
    cell.topLabel.text = city.firstname;
    cell.egLabel.text = city.secondname;
    cell.peopleLabel.text = city.beenstr;
#pragma warning 不写else爱出问题
    if (city.discounts.count != 0) {
        
        cell.priceLabel.text =  [[(city.discounts[0])[@"price"] stringByReplacingOccurrencesOfString:@"<em>" withString:@""]stringByReplacingOccurrencesOfString:@"</em>" withString:@""];
        
    }else {
        cell.priceLabel.text = @"";
    }
    
    //无半星情况
    if (city.gradescores%2 == 0) {
        for (NSInteger i = 0; i < city.gradescores/2; i++) {
            switch (i) {
                case 0:
                    [cell.imageViewOne setImage:[UIImage imageNamed:@"iconfont-zuixin-6"]];
                    
                    break;
                case 1:
                    [cell.imageViewTwo setImage:[UIImage imageNamed:@"iconfont-zuixin-6"]];
                    break;
                case 2:
                    [cell.imageViewThree setImage:[UIImage imageNamed:@"iconfont-zuixin-6"]];
                    break;
                case 3:
                    [cell.imageViewFour setImage:[UIImage imageNamed:@"iconfont-zuixin-6"]];
                    break;
                case 4:
                    [cell.imageViewFive setImage:[UIImage imageNamed:@"iconfont-zuixin-6"]];
                    break;
                default:
                    break;
            }
        }
        
        for (NSInteger i = 0; i < 5-city.gradescores/2; i++) {
            switch (i) {
                case 0:
                    [cell.imageViewFive setImage:[UIImage imageNamed:@"iconfont-zuixin-5"]];
                    break;
                case 1:
                    [cell.imageViewFour setImage:[UIImage imageNamed:@"iconfont-zuixin-5"]];
                    break;
                case 2:
                    [cell.imageViewThree setImage:[UIImage imageNamed:@"iconfont-zuixin-5"]];
                    break;
                case 3:
                    [cell.imageViewTwo setImage:[UIImage imageNamed:@"iconfont-zuixin-5"]];
                    break;
                case 4:
                    [cell.imageViewOne setImage:[UIImage imageNamed:@"iconfont-zuixin-5"]];
                    break;
                    
                default:
                    break;
            }
        }
        // 有半星的情况
    }else if (city.gradescores%2 == 1) {
        NSLog(@"老%ld和%ld",city.gradescores/2,indexPath.row);
        switch (city.gradescores/2) {
            case 1:
                [cell.imageViewTwo setImage:[UIImage imageNamed:@"iconfont-pingfenxingxingbankesvg-4"]];
                break;
            case 2:
                
                [cell.imageViewThree setImage:[UIImage imageNamed:@"iconfont-pingfenxingxingbankesvg-4"]];
                break;
            case 3:
                [cell.imageViewFour setImage:[UIImage imageNamed:@"iconfont-pingfenxingxingbankesvg-4"]];
                break;
            case 4:
                [cell.imageViewFive setImage:[UIImage imageNamed:@"iconfont-pingfenxingxingbankesvg-4"]];
                break;
            default:
                break;
        }
        
        
        for (NSInteger i = 0; i < city.gradescores/2; i++) {
            switch (i) {
                case 0:
                    [cell.imageViewOne setImage:[UIImage imageNamed:@"iconfont-zuixin-6"]];
                    
                    break;
                case 1:
                    [cell.imageViewTwo setImage:[UIImage imageNamed:@"iconfont-zuixin-6"]];
                    
                    break;
                case 2:
                    [cell.imageViewThree setImage:[UIImage imageNamed:@"iconfont-zuixin-6"]];
                    
                    break;
                case 3:
                    [cell.imageViewFour setImage:[UIImage imageNamed:@"iconfont-zuixin-6"]];
                    
                    
                    break;
                    
                default:
                    break;
            }
            
            
        }
        for (NSInteger i = 0; i < 5-city.gradescores/2; i++) {
            switch (i) {
                    
                case 1:
                    [cell.imageViewFive setImage:[UIImage imageNamed:@"iconfont-zuixin-5"]];
                    break;
                case 2:
                    [cell.imageViewFour setImage:[UIImage imageNamed:@"iconfont-zuixin-5"]];
                    break;
                case 3:
                    [cell.imageViewThree setImage:[UIImage imageNamed:@"iconfont-zuixin-5"]];
                    break;
                case 4:
                    [cell.imageViewTwo setImage:[UIImage imageNamed:@"iconfont-zuixin-5"]];
                    break;
                    
                default:
                    break;
            }
        }
        
    }

    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
       [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    CityModel *city = self.foodArr[indexPath.row];
    NSString *body = [NSString stringWithFormat:@"client_id=qyer_android&client_secret=9fcaae8aefc4f9ac4915&v=1&track_deviceid=863151025525517&track_app_version=6.7.1&track_app_channel=baidu&track_device_info=mx3&track_os=Android4.4.4&app_installtime=1444613810999&lat=38.883443&lon=121.544998&client_id=qyer_android&poi_id=%@&screensize=1800",city.mId];
    NSLog(@"我擦%@",body);
    
    NSDictionary *dic = @{@"Content-Type":@"application/x-www-form-urlencoded"};
    [ZSMAFNetWork posturl:@"http://open.qyer.com/qyer/footprint/poi_detail" body:body result:ZSMJSON requestStyle:ZSMRequestString headerFile:dic success:^(id result) {
        NSString *str =  result[@"data"][@"qyerurl"];
        
        DisCountWebView *web = [[DisCountWebView alloc]init];
        web.disCountStr = str;
        [self.navigationController pushViewController:web animated:YES];
    } failure:^(NSError *error) {
        
        NSLog(@"%@",error);
    }];
}

- (void)night {
    if ([SingletonNight shareSingletonNight].isSelete == YES) {
        self.tableView.backgroundColor = [UIColor blackColor];
        self.titleLabel.textColor = [UIColor blackColor];
    }
    else {
        self.tableView.backgroundColor = [UIColor whiteColor];
        self.titleLabel.textColor = [UIColor whiteColor];
    }
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"night" object:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
